<?php

namespace app\api\model;

use app\admin\model\usermanage\Users;
use think\Db;
use think\Exception;
use think\Model;

/**
 * 会员提现模型
 */
class ShareCash extends Model
{

    // 表名
    protected $name = 'share_cash';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
    ];


    public function apply($cash,$user_id,$name,$mobile,$bank_name)
    {
        if (empty($name)||empty($mobile)||empty($bank_name)){
            throw new Exception('缺少必填参数');
        }

        $cash_max_day = \app\common\model\Config::get(['name'=>'cash_max_day'])->value;

        if ($cash_max_day) {
            $starttime = strtotime(date('Y-m-d 00:00:00'));
            $endtime = strtotime(date('Y-m-d 23:59:59'));
            $cash_sum = ShareCash::where('status','in',[0, 1, 2, 5])
                ->where('createtime', 'between time', [$starttime, $endtime])
                ->sum('price');
            $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
            if ($cash > $cash_max_day) {
                throw new Exception('提现金额不能超过' . $cash_max_day . '元');
            }
        }

        $user = Users::get($user_id);
        if (!$user) {
            throw new Exception('网络异常');
        }

        $min_money = \app\common\model\Config::get(['name'=>'min_money'])->value;

        if ($cash < $min_money) {
            throw new Exception('提现金额不能小于' . $min_money . '元');
        }
        if ($user->price < $cash) {
            throw new Exception('提现金额不能超过剩余金额');
        }
        $exit = ShareCash::where('status', '=', 1)
            ->where(['user_id' => $user_id])->find();
        if ($exit) {
            throw new Exception('尚有未完成的提现申请');
        }

        Db::startTrans();
        try {
            $cash_model = new ShareCash();
            $data = [
                'order_no'=>$this->getOrderNo(),
                'status'=>1,
                'type'=>2,
                'price'=>$cash,
                'user_id'=>$user_id,
                'name'=>$name,
                'mobile'=>$mobile,
                'bank_name'=>$bank_name,
                'service_charge'=>$cash_max_day
            ];
            $res = $cash_model->save($data);
            if ($res) {

                $user->price -= $cash;
                if (!$user->save()) {
                    Db::rollback();
                    throw new Exception('网络异常');
                }

                Db::commit();
                return true;
            } else {
                Db::rollback();
                throw new Exception('网络异常');
            }
        }  catch (Exception $e) {
            Db::rollback();
            throw new Exception($e->getMessage());
        }
    }

    private function getOrderNo()
    {
        $order_no = null;
        while (true) {
            $order_no = date('YmdHis') . mt_rand(100000, 999999);
            $exist_order_no = ShareCash::where(['order_no' => $order_no])->find();
            if (!$exist_order_no) {
                break;
            }
        }
        return $order_no;
    }
}
